package com.cdshangpai.utils.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.cdshangpai.utils.db.beans.Account;

import java.util.ArrayList;

public class AccountDb {
    SQLiteDatabase db=DatabaseHelper.getDb();
    String tablename="t_account";

     public long insert(Account account)
     {
         long id=db.insert(tablename,null,account.getValues());

         return id;
     }

     public void update(Account account)
     {
        db.update(tablename,account.getValues()," id=?",new String[]{account.id.toString()});
     }

     public void delete(Integer id)
     {
         db.delete(tablename," id=?",new String[]{id.toString()});
     }

     public void clean()
     {
         String sql="delete from "+tablename;
         db.execSQL(sql);
     }

     public Account queryById(Integer id)
     {
        String sql="select * from "+tablename+" where id="+id;
        Cursor c=db.rawQuery(sql,null);
        c.moveToFirst();
        if(c.isAfterLast())
        {
            return null;
        }
        else
        {
            return cToAccount(c);
        }
     }



     public ArrayList<Account> query(String account,String password)
     {
         ArrayList<Account> result=new ArrayList<>();
         String sql="select * from "+tablename;
         StringBuilder where=new StringBuilder();
         ArrayList<String> wheres=new ArrayList<>();

         if(!TextUtils.isEmpty(account))
         {
             where.append(" `account`=? and ");
             wheres.add(account);
         }
         if(!TextUtils.isEmpty(password))
         {
             where.append(" `password`=? and ");
             wheres.add(password);
         }

         Cursor c=null;
         if(wheres.size()>0)
         {
             Integer pos=where.lastIndexOf("and");
             sql=sql+=" where "+where.substring(0,pos);
             c=db.rawQuery(sql,wheres.toArray(new String[wheres.size()]));
         }
         else
         {
             c=db.rawQuery(sql,null);
         }

         c.moveToFirst();
         while (!c.isAfterLast())
         {
             result.add(cToAccount(c));
             c.moveToNext();
         }
         c.close();
         return result;
     }

     private Account cToAccount(Cursor c)
     {
         Account account=new Account();
         account.id=c.getInt(c.getColumnIndex("id"));
         account.account=c.getString(c.getColumnIndex("account"));
         account.password=c.getString(c.getColumnIndex("password"));
         account.name=c.getString(c.getColumnIndex("name"));
         account.area=c.getString(c.getColumnIndex("area"));
         account.version=c.getString(c.getColumnIndex("version"));
         return account;
     }

     public void test()
     {
         clean();

         Account account=new Account();
         account.account="account";
         account.password="password";
         account.name="name";
         account.version="version";

         Long id=insert(account);

         account.id=id.intValue();
         account.account="accountupdate";
         update(account);


         ArrayList<Account> ls=query("","");

         ls=query("accountupdate","password");

         account=queryById(id.intValue());

         delete(id.intValue());

         ls=query("","");


     }
}
